package com.wiserun.RestFul.controller;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.wiserun.RestFul.service.FileRestService;
import com.wiserun.common.model.ResponseData;
import com.wiserun.common.util.FormatUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(description = "restful文件接口")
@RestController
@RequestMapping("/fileRest")
public class fileRest {
	private static final Logger logger = LoggerFactory.getLogger(fileRest.class);
	
	@Autowired
	private FileRestService service;
	
	@Value("${fileConvert.filePath}")
	private String localPath="/pdfPaseTemp/";
	@Value("${fileConvert.delTemp}")
	private boolean delTemp=true;//转换后是否删除文件

	//  http://192.168.111.45:8080/fileRest/preView
	//  http://192.168.111.45:8080/wis-new/fileRest/preView
	// http://192.168.168.235:8080/wis-new/fileRest/preView
	// http://192.168.168.225:8080/wis-new/fileRest/preView
	@ApiOperation(value = "文件预览", notes = "文件预览")
    @RequestMapping(value = "/preView", method = { RequestMethod.POST },produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseData preView(@RequestBody Map<String, Object> params) {
		Map<String, Object> map = new HashMap<>();
		try {
			String oldPath=params.get("fileName").toString();//流文件路径
			String fileName="";//文件名称
			String fileData=params.get("fileData").toString();//接收到的文件流
			String pdfPath="";//pdf路径
			String outDate="";//输出数据流
			String filePath="";//流转换成文档的路径
			//logger.info("原始oldPath=>"+oldPath);
			//logger.info(fileName.substring(fileName.indexOf(".php")+5));
			oldPath=URLDecoder.decode(oldPath,"UTF-8");
			fileName=oldPath.substring(oldPath.indexOf(".php")+5,oldPath.lastIndexOf("?"));
			//logger.info("filePath_befor=>"+filePath);
			filePath= localPath+fileName;
			//logger.info("filePath_after=>"+filePath);
			fileName=fileName.substring(fileName.lastIndexOf("/")+1);
			//logger.info(fileName);
			String beReplace=fileData.substring(0,fileData.lastIndexOf("base64,")+7);
			//logger.info(beReplace);
			fileData=fileData.replace(beReplace,"");
			service.base64StringToFile(fileData,filePath);
			//文件已保存到服务器磁盘
			pdfPath=filePath.substring(0,filePath.lastIndexOf("."))+".pdf";
			//logger.info("pdfPath"+pdfPath);
			if(service.whichFile2PDF(fileName,filePath,pdfPath)) {
				outDate=service.encryptToBase64(pdfPath);
				map.put("fileData",outDate);
			}
			if(delTemp) {
				service.deleteTempFile(filePath,pdfPath);
			}
		}catch(Exception e) {
			logger.error("出错了");
			e.printStackTrace();
			return FormatUtil.fail(map);
		}
		
		return FormatUtil.success(map);
	}
}
